rm(list =ls())
options(scipen=999)
gc()
packages <-c("tidyverse","estimatr","plm","stargazer","fastDummies","ICCbin","ihs")

new.packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

lapply(packages, require, character.only = TRUE)
rm(packages)

setwd("PUT YOUR PATH HERE")

## insheet data
data <- read.csv("./Datasets/panel_bare_bones.csv")

## clean data
data$X <- NULL
data$locality_year <- NULL
data$subbotnik_inkind <- NULL

# generate dummies for year and locality
data$year <- as.character(data$year)
data$locality <- as.character(data$locality)
data <- fastDummies::dummy_cols(data)
data$year <- as.numeric(data$year)


###############################
###############################
### REGRESSIONS FOR STRIKES ###
###############################
###############################

#subset to years with observations:
data_t <- data[which(data$year>1979),]

#scale outcome
data_t$strikes <- scale(data_t$strikes)

### simple OLS
m_repression_ols <- lm(strikes ~ commanders, data = data_t)

### Unit FEs
m_repression_id_fe <- plm(strikes ~ commanders, data = data_t, index = c("locality"), model = "within")

### Time FEs
m_repression_year_fe <- plm(strikes ~ commanders, data = data_t, index = c("year"), model = "within")

### Unit and Time FEs 

m_repression_year_locality_fe <- plm(strikes ~ commanders, data = data_t, index = c("year", "locality"), model = "within", effect = "twoways")


########################################
########################################
## REGRESSIONS FOR SUBBOTNIK (Inkind) ##
########################################
########################################

# subset to years with observations:
data_t <- data[which(data$year<1980),]

# remove missings
data_t <- data_t[,c("locality_numeric", "year", "commanders", "subbotnik_inkind_", "year_1975", "year_1976", "year_1977", "year_1978", "year_1979")]
data_t <- na.omit(data_t) 

# turn subbotnik upside down such that it reflects sabotage
data_t$subbotnik_inkind_ <- data_t$subbotnik_inkind_*-1

#scale outcome
data_t$subbotnik_inkind_ <- scale(data_t$subbotnik_inkind_)

## simple OLS
m_subbotnik_inkind_ols <- lm(subbotnik_inkind_ ~ commanders, data = data_t)


## Unit FEs 
#NB: CAN'T SENSIBLY MODEL THIS BECAUSE EFFECTIVE SAMPLE IS N = 2

## Time FEs
m_subbotnik_inkind_year_fe <- plm(subbotnik_inkind_ ~ commanders, data = data_t, index = c("year"), model = "within")


############
## Output ##
############

stargazer(m_repression_ols, m_repression_year_fe, 
          m_repression_year_locality_fe,  m_repression_year_locality_fe, 
          m_subbotnik_inkind_ols, m_subbotnik_inkind_year_fe,
          dep.var.labels = c("Protest (z)", "Resistance (z)"),
          style = "qje",
          covariate.labels = c("Repression (cont.)"),
          star.char = c("*", "**", "***"),
          star.cutoffs = c(0.1, 0.05, 0.01),
          omit = c("Constant"),
          omit.stat = c("rsq", "f", "ser"), 
          omit.table.layout = "n",
          add.lines = list(c("Year fixed effects", "No", "Yes", "No", "Yes", "No", "Yes"),
                           c("Locality fixed effects", "No", "No", "Yes", "Yes", "No", "No"))),
          out = "PUT YOUR FILE PATH HERE")


